list1 = [ 'william', 1999, 'weng', 25 ]
list1[-1] = True
# IndexError: list assignment index out of range
# list1[4] = 'hyoka'
list1.append(8530)
print(list1)
# ['william', 1999, 'weng', True, 8530]

del list1[2]
print(list1)
# ['william', 1999, True, 8530]

# 补充一些操作
print(len(list1))   # 4
print(list1 * 2)    # ['william', 1999, True, 8530, 'william', 1999, True, 8530]
for ele in list1: print(ele, end=""); 
# william1999True8530
print()

# 嵌套的时候
list2 = [1, 2, [7, 8, 9], 4]
print(list2[2], list2[2][1]) # [7, 8, 9] 8

# 比较
import operator
list3 = [3, 4]; list4 = [3, 4]
print(operator.eq(list1, list2))    # False
print(operator.eq(list3, list4))    # True

# 复制
list5 = list1
list6 = list1[::]
list7 = list1[:]
import copy; list8 = copy.copy(list1)
print(list1, list5, list6, list7, list8)
# ['william', 1999, True, 8530] ['william', 1999, True, 8530] ['william', 1999, True, 8530] ['william', 1999, True, 8530] ['william', 1999, True, 8530]
print(id(list1), id(list5), id(list6), id(list7), id(list8)) 
# 1433368385856 1433368385856 1433370694592 1433370703936 1433370703616

# None 为py内建的值，表不存在的
print([None]*10)
# [None, None, None, None, None, None, None, None, None, None]

# 推导式 创建list
list9 = [1, 2 ,3, 4, 5, 6, 7, 8, 9]
print([x**2 for x in list9 if x > 5]) # [36, 49, 64, 81]
print(dict([ (x, x*10) for x in list9 if x <= 5 ])) # {1: 10, 2: 20, 3: 30, 4: 40, 5: 50}
print([ (x,y) for x in range(10) if x%2 if x>3 for y in range(10) if y>7 if y != 8  ]) # [(5, 9), (7, 9), (9, 9)]

list10 = [2, 4, 6]; list11 = [4, 3, -9]
print([ list10[i]+list11[i] for i in range(len(list10)) ])  # [6, 7, -3]
print([ x*y for x in [1,2,3] for y in [1,2,3] ])            # [1, 2, 3, 2, 4, 6, 3, 6, 9]

list12 = range(4); print(list12)    # range(0, 4) range() 返回的是 可迭代对象
def aFn(x):
    return x*2
print([ aFn(i) for i in list12 ])   # [0, 2, 4, 6]

# range(stop)
# range(start, stop[, step])

# TypeError: object of type 'int' has no len()
# list13=['a','ab','3ae','zaaa',1]
list13=['a','ab','3ae','zaaa','1']
print(id(list13)) # 2327150599168
print(list13.sort(key=len)) # None 没有返回值，不会报错，还是执行了
print(list13, id(list13))
# ['a', '1', 'ab', '3ae', 'zaaa'] 2327150599168
